﻿unit UReminder;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
  System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
  Vcl.StdCtrls, Vcl.ExtCtrls, Vcl.Buttons, Vcl.ComCtrls;

type
  TReminderForm = class(TForm)
    pnlMain: TPanel;
    pnlBottom: TPanel;
    lblTitle: TLabel;
    lblContent: TLabel;
    lblRemindTime: TLabel;
    memContent: TMemo;
    btnComplete: TBitBtn;
    btnPostpone: TBitBtn;
    btnClose: TBitBtn;
    cbbPostpone: TComboBox;
    tmrFlash: TTimer;
    
    procedure FormCreate(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure tmrFlashTimer(Sender: TObject);
    procedure btnCompleteClick(Sender: TObject);
    procedure btnPostponeClick(Sender: TObject);
    procedure btnCloseClick(Sender: TObject);
  private
    FMemoID: Integer;
    FReminderID: Integer;
    FFlashCount: Integer;
    procedure InitializePostponeItems;
    procedure PlayRemindSound;
    procedure UpdateReminderStatus(Status: Integer);
    procedure PostponeReminder(Minutes: Integer);
  public
    class procedure ShowReminder(const Title, Content: string;
      RemindTime: TDateTime; MemoID: Integer = 0; ReminderID: Integer = 0);
  end;

implementation

{$R *.dfm}

uses UDataModule;

{ TReminderForm }

class procedure TReminderForm.ShowReminder(const Title, Content: string;
  RemindTime: TDateTime; MemoID: Integer = 0; ReminderID: Integer = 0);
var
  Form: TReminderForm;
begin
  Form := TReminderForm.Create(nil);
  try
    Form.FMemoID := MemoID;
    Form.FReminderID := ReminderID;
    Form.lblTitle.Caption := Title;
    Form.memContent.Text := Content;
    Form.lblRemindTime.Caption := '提醒时间: ' + FormatDateTime('yyyy-mm-dd hh:nn', RemindTime);
    Form.Show;
  except
    Form.Free;
  end;
end;

procedure TReminderForm.FormCreate(Sender: TObject);
begin
  InitializePostponeItems;
  FFlashCount := 0;
  tmrFlash.Enabled := True;
  PlayRemindSound;
end;

procedure TReminderForm.InitializePostponeItems;
begin
  with cbbPostpone.Items do
  begin
    Clear;
    Add('5分钟后');
    Add('10分钟后');
    Add('30分钟后');
    Add('1小时后');
    Add('2小时后');
  end;
  cbbPostpone.ItemIndex := 0;
end;

procedure TReminderForm.PlayRemindSound;
begin
  MessageBeep(MB_ICONEXCLAMATION);
end;

procedure TReminderForm.FormShow(Sender: TObject);
begin
  // 将窗体显示在屏幕右下角
  Left := Screen.Width - Width - 20;
  Top := Screen.Height - Height - 40;
end;

procedure TReminderForm.tmrFlashTimer(Sender: TObject);
begin
  Inc(FFlashCount);
  if FFlashCount mod 2 = 0 then
    Color := clBtnFace
  else
    Color := $00E6E6FF;  // 浅黄色
    
  if FFlashCount >= 6 then
  begin
    tmrFlash.Enabled := False;
    Color := clBtnFace;
  end;
end;

procedure TReminderForm.UpdateReminderStatus(Status: Integer);
begin
  if FReminderID > 0 then
  begin
    with DM.qryReminder do
    begin
      Close;
      SQL.Text := 'UPDATE Reminder SET Status = :Status WHERE ID = :ID';
      Parameters.ParamByName('Status').Value := Status;
      Parameters.ParamByName('ID').Value := FReminderID;
      ExecSQL;
    end;
  end;
end;

procedure TReminderForm.PostponeReminder(Minutes: Integer);
var
  NewRemindTime: TDateTime;
begin
  NewRemindTime := Now + (Minutes / 1440); // 转换分钟为日期时间
  
  with DM.qryReminder do
  begin
    Close;
    SQL.Text := 'UPDATE Reminder SET RemindTime = :RemindTime, Status = 0 ' +
                'WHERE ID = :ID';
    Parameters.ParamByName('RemindTime').Value := NewRemindTime;
    Parameters.ParamByName('ID').Value := FReminderID;
    ExecSQL;
  end;
end;

procedure TReminderForm.btnCompleteClick(Sender: TObject);
begin
  // 更新备忘录状态为已完成
  with DM.qryMemo do
  begin
    Close;
    SQL.Text := 'UPDATE Memo SET Status = 1 WHERE ID = :ID';
    Parameters.ParamByName('ID').Value := FMemoID;
    ExecSQL;
  end;
  
  // 更新提醒状态为已关闭
  UpdateReminderStatus(2);
  Close;
end;

procedure TReminderForm.btnPostponeClick(Sender: TObject);
const
  PostponeMinutes: array[0..4] of Integer = (5, 10, 30, 60, 120);
begin
  if cbbPostpone.ItemIndex >= 0 then
  begin
    PostponeReminder(PostponeMinutes[cbbPostpone.ItemIndex]);
    Close;
  end;
end;

procedure TReminderForm.btnCloseClick(Sender: TObject);
begin
  UpdateReminderStatus(1); // 设置为已提醒
  Close;
end;

end. 